<?php
/*
/**
* Index 模块服务接口
*/

namespace app\api\controller;

use think\Db;

class Index extends Common
{

    /**
     * 摊大饼
     * @desc 摊大饼策略根据节假日决定是否爬取数据
     */
    public function index()
    {
        //    dump(date('y-m-d h:i:s',time()));die;
        // try {
        //     $this->getData();
        // } catch (\Exception $e) {
        //     email(" 我的服务器", "可转债运行bug", $e->getMessage() . "行号:" . $e->getLine() . $e->getTraceAsString());
        // }
        $isholiday = Db::table('group')->where('group_id', 1)->value('holiday');
        if ($isholiday == 2) {
            echo '节假日休市';
        } elseif ($isholiday == 1) {
            echo '休息日休市';
        } else {
            $week = date("w", time());
            if ($week == 1 || $week == 2 || $week == 3 || $week == 4 || $week == 5) {
                $seconds = rand(1, 60);
                sleep($seconds);
                $morning = strtotime('09:30');
                $morningend = strtotime('11:32');
                $afternoon = strtotime('13:00');
                $afternoonend = strtotime('15:02');
                if (time() > $morning && time() < $morningend || time() > $afternoon && time() < $afternoonend) {
                    try {
                        $this->getData();
                    } catch (\Exception $e) {
                        email(" 我的服务器", "可转债运行bug", $e->getMessage() . "行号:" . $e->getLine() . $e->getTraceAsString());
                    }
                } else {
                    //溢价率前10低于170
                    // try {
                    //     $this->getData10();
                    // } catch (\Exception $e) {
                    //     email(" 我的服务器", "可转债运行bug", $e->getMessage() . "行号:" . $e->getLine() . $e->getTraceAsString());
                    // }
                }
            } else {
                echo '周末休市';
            }
        }
    }


    /**
     * 获取前10数据
     * @desc 获取当前转债数据，并加以组装
     */
    public function getData10()
    {
        //只买不强赎的低于170的折价前五，超过前十或者强赎就卖出，前10名内符合条件的标的有3个不在仓库里就调仓。
            $morningend = strtotime('11:35');
            $afternoon = strtotime('12:00');
            if ($morningend < time() && $afternoon > time()) {
                header('Content-type:text/html;charset=utf-8');
                list($t1, $t2) = explode(' ', microtime());
                $t = (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
                $url = 'https://www.jisilu.cn/data/cbnew/cb_list/?___jsl=LST___t=' . $t;
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_HEADER, "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1");
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                $html = curl_exec($ch);
                curl_close($ch);
                $datas = json_decode($html, true)['rows'];
                $qiangshu = []; //存储已经强生公告的
                foreach ($datas as $key => $value) { //qstatus=03是停牌

                    if ($value['redeem_flag'] == 'Y') {
                        $qiangshu[] = $value['bond_id'];
                    }
                    //折价
                    if (
                        $value['cell']['btype'] == 'E' ||
                        $value['cell']['redeem_flag'] == 'Y' ||
                        $value['cell']['price_tips'] == '待上市' ||
                        $value['cell']['qstatus'] == '03'
                    ) { //停牌
                    } else {
                        $sell_price[] = [
                            'bond_id' => $value['cell']['bond_id'],
                            'dblow' => $value['cell']['dblow'],
                            'bond_name' => $value['cell']['bond_nm'],
                            'curr_iss_amt' => $value['cell']['curr_iss_amt'],
                            'price' => $value['cell']['price'],
                            'premium_rt' => (float)$value['cell']['premium_rt'], //溢价率
                            'sincrease_rt' => (float)$value['cell']['sincrease_rt'], //正股涨跌幅
                            'rating_cd' => $value['cell']['rating_cd'],
                        ];
                    }
                }

                $is_one = Db::table('group')->where('group_id', 3)->value('is_one');
                if ($is_one != 1) {
                    $this->low_premium_rt($sell_price, $qiangshu);
                } else {
                    Db::table('group')
                        ->where('group_id', 3)
                        ->update([
                            'is_one' => 2,
                        ]);
                }
            }
    }

    /**
     * 获取数据
     * @desc 获取当前转债数据，并加以组装
     */
    public function getData()
    {
        $enddata = jisilu_east();
        $finaldata = []; //存储大饼需要数据
        $median = []; //存储中位数
        $qiangshu = []; //存储已经强生公告的 

        foreach ($enddata as $key => $value) { //qstatus=03是停牌

            if ($value['redeem_flag'] == 'Y') {
                $qiangshu[] = $value['bond_id'];
            }
            if ($value['btype'] == 'E' || $value['price_tips'] == '待上市') {
            } else {
                $median[] = [
                    'bond_id' => $value['bond_id'],
                    'bond_name' => $value['bond_nm'],
                    'price' => $value['price'],
                    'premium_rt' => (float)$value['premium_rt'],
                    'dblow' => $value['dblow'],
                ];
            }


            //大饼用
            if (
                $value['btype'] == 'E' ||
                $value['price_tips'] == '待上市' ||
                $value['curr_iss_amt'] > 30 ||
                $value['redeem_flag'] == 'Y' ||
                $value['premium_rt'] > 45 ||
                //                $value['cell']['rating_cd'] == 'B' ||
                //                $value['cell']['rating_cd'] == 'BB' ||
                //                $value['cell']['rating_cd'] == 'BBB' ||
                $value['qstatus'] == '03'
            ) {
                unset($enddata[$key]);
            } else {
                $finaldata[] = [
                    'bond_id' => $value['bond_id'],
                    'bond_name' => $value['bond_nm'],
                    'curr_iss_amt' => $value['curr_iss_amt'],
                    'price' => $value['price'],
                    'premium_rt' => (float)$value['premium_rt'],
                    'dblow' => $value['dblow'],
                    'rating_cd' => $value['rating_cd'],
                ];
            }


            //折价
            if (
                $value['btype'] == 'E' ||
                $value['price_tips'] == '待上市' ||
                $value['redeem_flag'] == 'Y' ||
                $value['qstatus'] == '03' ||
                $value['rating_cd'] == 'B' ||
                $value['rating_cd'] == 'BB' ||
                $value['rating_cd'] == 'BBB-' ||
                $value['rating_cd'] == 'BBB'
            ) { //停牌
                unset($enddata[$key]);
            } else {
                $sell_price[] = [
                    'bond_id' => $value['bond_id'],
                    'dblow' => $value['dblow'],
                    'bond_name' => $value['bond_nm'],
                    'curr_iss_amt' => $value['curr_iss_amt'],
                    'price' => $value['price'],
                    'premium_rt' => (float)$value['premium_rt'], //溢价率
                    'sincrease_rt' => (float)$value['sincrease_rt'], //正股涨跌幅
                    'rating_cd' => $value['rating_cd'],
                ];
            }
        }

        //实时查询前5里面有没有强赎
        //拿到仓库最新的
        $it_five = Db::table('dblow_premium')
            ->where('status', 1)
            ->select();

        $redeem = [];
        //先判断是否仓库是否有强赎的
        foreach ($it_five as $key => $value) {

            if (in_array($value['bond_id'], $qiangshu)) {
                $redeem[] = $value;
            }
        }

        $redeem_name = [];
        if (count($redeem) > 0) {
            foreach ($redeem as $k => $v) {
                // 启动事务
                Db::startTrans();
                Db::table('dblow_premium')
                    ->where('bond_id', $v['bond_id'])
                    ->where('status', 1)
                    ->update(['status' => 2, 'new_price' => $v['price']]);
                // 提交事务
                Db::commit();
                $redeem_name[] = $v['bond_name'];
            }


            $mailtext = '卖出:' . PHP_EOL . implode(PHP_EOL, $redeem_name);
            $res = email("前5", "前5强赎卖出", $mailtext);
            if ($res == 200) {
                Db::startTrans();
                Db::table('dblow_premium')
                    ->where('status', 2)
                    ->update(['status' => 4, 'updated_at' => time()]);
                Db::commit();
                echo '前5强赎卖出成功';
            }
        }


        $sandi = $tan = $gt100andlt115 = $three = $four = $finaldata;
        //象限2，低价格，高溢价债性强容易变成白天鹅（债性）（收益太低，暂时不用了，之前的全清仓，改成妖债潜质的2021.5.24）
        $gt100andlt115 = $this->gt100andlt115($sell_price, $qiangshu);

        //象限3,低价格低溢价债性强股性强(双低)
        $double_low = $this->three($three, $qiangshu);

        //象限4,高价格低溢价股性强
        $low_rate = $this->four($four, $qiangshu);

        //结合溢价率和价格
        $tan = $this->tan($tan, $qiangshu);


        //三低玩法
        $tan = $this->threeLow($sandi, $qiangshu);


        //计算中位数
        if (time() > strtotime('15:00')) {
            $this->median($median);
        }

        /** start
         * 更新实时数据
         */

        //更新折价率
        $it_exist_sell_price = Db::table('sell_price')
            ->where('status', 1)
            ->select();
        foreach ($it_exist_sell_price as $key => $value) {
            foreach ($sell_price as $vv) {
                if ($value['bond_id'] == $vv['bond_id']) {
                    Db::table('sell_price')
                        ->where('status', 1)
                        ->where('bond_id', $value['bond_id'])
                        ->update([
                            'time_price' => $vv['price'],
                            'time_premium_rt' => $vv['premium_rt'],
                            'time_sincrease_rt' => $vv['sincrease_rt'],
                        ]);
                }
            }
        }
        //更新大饼最新价格
        $hose = Db::table('bonds_data')
            ->where('status', 1)
            ->select();
        foreach ($hose as $value) {
            foreach ($sell_price as $v) {
                if ($value['bond_id'] == $v['bond_id']) {
                    Db::table('bonds_data')
                        ->where('status', 1)
                        ->where('bond_id', $value['bond_id'])
                        ->update([
                            'time_dblow' => $v['dblow'],
                            'sincrease_rt' => $v['sincrease_rt'],
                            'curr_iss_amt' => $v['curr_iss_amt'],
                            'time_premium_rt' => $v['premium_rt'],
                            'time_price' => $v['price'],
                        ]);
                }
            }
        }

        /** end
         *
         */

        //折价组合每周二中午调仓
        $week = date("w", time());
        if ($week == 2) {
            $morningend = strtotime('11:35');
            $afternoon = strtotime('12:00');
            if ($morningend < time() && $afternoon > time()) {
                $is_one = Db::table('group')->where('group_id', 2)->value('is_one');
                if ($is_one != 1) {
                    $this->sell_price($sell_price, $qiangshu);
                }
            } else {
                Db::table('group')
                    ->where('group_id', 2)
                    ->update([
                        'is_one' => 2,
                    ]);
            }
        }

        //发送邮件

        //全部为3是建仓
        $status = Db::table('bonds_data')
            ->column('status');

        $status = array_unique($status);
        if (in_array('2', $status)) {
            //卖出数据拿出来
            $sellhose = Db::table('bonds_data')
                ->where('status', 2)
                ->select();

            //买入数据拿出来
            $buyhose = Db::table('bonds_data')
                ->where('status', 3)
                ->select();

            $sell = [];
            $buy = [];
            if (empty($buyhose) && empty($sellhose)) {
                echo '摊大饼没达到调仓条件';
            } else {
                //换成邮件内容需要的数组
                foreach ($buyhose as $key => $value) { //买入
                    //type 1:高值底2:双低3:低溢价4高评级低溢价5高评级低价格
                    switch ($value['type']) {
                        case 1:
                            $type = '低价格';
                            break;
                        case 2:
                            $type = '双低';
                            break;
                        case 3:
                            $type = '低溢价';
                            break;
                        case 4:
                            $type = '潜力妖债';
                            break;
                        case 5:
                            $type = '高评级低价格';
                            break;
                        case 10:
                            $type = '鲨鱼式摊大饼';
                            break;
                        case 30:
                            $type = '三低玩法';
                            break;
                        default:
                            $type = '未知';
                            break;
                    }

                    $buy[] = '代码：' . $value['bond_id'] . ' 名字：' . $value['bond_name'] . '买入价格：' . $value['price'] . '溢价率：' . $value['premium_rt'] . ' 类型：' . $type;
                }
                foreach ($sellhose as $key => $value) { //卖出
                    //type 1:高值底2:双低3:低溢价4高评级低溢价5高评级低价格
                    switch ($value['type']) {
                        case 1:
                            $type = '低价格';
                            break;
                        case 2:
                            $type = '双低';
                            break;
                        case 3:
                            $type = '低溢价';
                            break;
                        case 4:
                            $type = '潜力妖债';
                            break;
                        case 5:
                            $type = '高评级低价格';
                            break;
                        case 10:
                            $type = '鲨鱼式摊大饼';
                            break;
                        case 30:
                            $type = '三低玩法';
                            break;
                        default:
                            $type = '未知';
                            break;
                    }

                    $sell[] = '代码：' . $value['bond_id'] . ' 名字：' . $value['bond_name'] . ' 买入价格：' . $value['price'] . ' 卖出价格：' . $value['new_price'] . '溢价率：' . $value['time_premium_rt'] . ' 类型：' . $type;
                }
                $mailtext = '卖出:' . PHP_EOL . implode(PHP_EOL, $sell) . PHP_EOL . "买入" . PHP_EOL . implode(PHP_EOL, $buy);
                $res = email("摊大饼", "调仓", '卖出' . count($sell) . "支买入" . count($buy) . "支明细：" . PHP_EOL . $mailtext);
                if ($res == 200) {
                    Db::startTrans();
                    Db::table('bonds_data')
                        ->where('status', 3)
                        ->update(['status' => 1]);

                    Db::table('bonds_data')
                        ->where('status', 2)
                        ->update(['status' => 4, 'updated_at' => date('Y-m-d H:i:s')]);

                    Db::table('mail_log')
                        ->insert([
                            'title' => '调仓',
                            'content' => $mailtext,
                            'buy' => count($buy),
                            'sell' => count($sell),
                            'create_at' => date('Y-m-d H:i:s'),
                        ]);

                    Db::commit();
                    echo '摊大饼调仓成功';
                }
            }
        } elseif (in_array('3', $status)) {

            //建仓数据拿出来
            $createhose = Db::table('bonds_data')
                ->where('status', 3)
                ->select();


            //换成邮件内容需要的数组
            foreach ($createhose as $key => $value) {
                //type 1:高值底2:双低3:低溢价4高评级低溢价5高评级低价格
                switch ($value['type']) {
                    case 1:
                        $type = '高债底';
                        break;
                    case 2:
                        $type = '双低';
                        break;
                    case 3:
                        $type = '低溢价';
                        break;
                    case 4:
                        $type = '潜力妖债';
                        break;
                    case 5:
                        $type = '高评级低价格';
                        break;
                    case 10:
                        $type = '鲨鱼式摊大饼';
                        break;
                    case 30:
                        $type = '三低玩法';
                        break;
                    default:
                        $type = '未知';
                        break;
                }

                $buy[] = '代码：' . $value['bond_id'] . ' 名字：' . $value['bond_name'] . ' 价格：' . $value['price'] . ' 类型：' . $type;
            }
            $mailtext = "建仓:" . PHP_EOL . implode(PHP_EOL, $buy);
            $res = email("摊大饼", "建仓", "共" . count($buy) . "支明细：" . PHP_EOL . $mailtext);
            if ($res == 200) {
                Db::startTrans();
                Db::table('bonds_data')
                    ->where('status', 3)
                    ->update(['status' => 1]);

                Db::table('mail_log')
                    ->insert([
                        'title' => '建仓',
                        'content' => $mailtext,
                        'buy' => count($buy),
                        'create_at' => date('Y-m-d H:i:s'),
                    ]);

                Db::commit();

                echo '建仓成功';
            }
        } else {
            echo '没触发邮件';
        }
    }

    /**
     *
     * @desc 去重二维数组
     */
    function array_unique_fb($array2D)
    {

        foreach ($array2D as $key => $v) {
            if (isset($temp[$v['bond_id']]) == false) {
                $temp[$v['bond_id']] = $v;
            }
        }

        return $temp;
    }

    /**
     *
     * @desc 获取仓库以外的
     */
    public
    function outside($finaldata, $num)
    {

        //获取仓库中存在的债编码
        $bond_ids = Db::table('bonds_data')
            ->where('status', 'in', [1, 3])
            ->where('type', $num)
            ->column('bond_id');

        //拿到除了仓库里的最新数据
        $outside = [];
        foreach ($finaldata as $key => $value) {
            if (!in_array($value['bond_id'], $bond_ids)) {
                $outside[] = $value;
            }
        }
        return $outside;
    }
    /**
     *
     * @desc 获取仓库以外的
     */
    public function outside100115($finaldata)
    {

        //获取仓库中存在的债编码
        $bond_ids = Db::table('gt100andlt115')
            ->where('status', 1)
            ->column('bond_id');

        //拿到除了仓库里的最新数据
        $outside = [];
        foreach ($finaldata as $key => $value) {
            if (!in_array($value['bond_id'], $bond_ids)) {
                $outside[] = $value;
            }
        }
        return $outside;
    }

    /**
     *
     * @desc 获取仓库以外的
     */
    public
    function outsides($finaldata)
    {

        //获取仓库中存在的债编码
        $bond_ids = Db::table('dblow_premium')
            ->where('status', 'in', [1, 3])
            ->column('bond_id');

        //拿到除了仓库里的最新数据
        $outside = [];
        foreach ($finaldata as $key => $value) {
            if (!in_array($value['bond_id'], $bond_ids)) {
                $outside[] = $value;
            }
        }
        return $outside;
    }

    /**
     *
     * @desc 获取仓库以外的
     */
    public
    function outsideone($finaldata)
    {

        //获取仓库中存在的债编码
        $bond_ids = Db::table('former_one')
            ->where('status', 'in', [1, 3])
            ->column('bond_id');

        //拿到除了仓库里的最新数据
        $outside = [];
        foreach ($finaldata as $key => $value) {
            if (!in_array($value['bond_id'], $bond_ids)) {
                $outside[] = $value;
            }
        }
        return $outside;
    }

    /**
     * 
     * 价格小于115买入，盈利超过100卖出
     */
    public function gt100andlt115($finaldata, $qiangshu)
    {
        //删除数组多余字段
        $update = $finaldata;
        foreach ($finaldata as $key => $value) {
            if ($value['curr_iss_amt'] <= 5 && $value['price'] <= 118 && $value['rating_cd'] <= 118) {
                unset($finaldata[$key]['premium_rt']);
                unset($finaldata[$key]['dblow']);
                unset($finaldata[$key]['rating_cd']);
                unset($finaldata[$key]['sincrease_rt']);
            } else {
                unset($finaldata[$key]);
            }
        }

        //先排序价格最低到最高
        $price = array_column($finaldata, 'price');
        array_multisort($price, SORT_ASC, $finaldata);
        //获取仓库以外的
        $outside = $this->outside100115($finaldata);
        $clone = $outside;
        $outsideData = [];
        foreach ($clone as $key => $value) {
            if ($value['price'] <= 118 && $value['curr_iss_amt'] <= 5) {
                $outsideData[] = $value;
            }
        }
        $outside = [];
        $outside = $outsideData;
        $outsideprice = array_column($outside, 'price');
        array_multisort($outsideprice, SORT_ASC, $outside);
        //拿到仓库最新的
        $gt100andlt115 = Db::table('gt100andlt115')
            ->where('status', 1)
            ->select();
        //先排序价格最低到最高
        $price = array_column($gt100andlt115, 'price');
        array_multisort($price, SORT_ASC, $gt100andlt115);

        //计算盈利
        $totalearned = array_sum(array_column($gt100andlt115, 'earned'));

        if (count($gt100andlt115) > 0) {
            //判断是否需要买新债
            if ($totalearned >= 1000) {
                if ((floor($totalearned / 1000) - (count($gt100andlt115) - 10)) > 0) {
                    for ($x = 0; $x < (floor($totalearned / 1000) - (count($gt100andlt115) - 10)); $x++) {
                        if (isset($outside[$x])) {
                            $ltInsertData[$x] = $outside[$x];
                            $ltInsertData[$x]['created_at'] = date('Y-m-d H:i:s');
                            Db::table('gt100andlt115')->insert($ltInsertData[$x]);
                        } else {
                            //先排序价格最低到最高
                            $price = array_column($gt100andlt115, 'price');
                            array_multisort($price, SORT_ASC, $gt100andlt115);
                            foreach ($gt100andlt115 as $key => $value) {
                                if ($value['add_ware'] < 3) {
                                    // 启动事务
                                    Db::startTrans();
                                    Db::table('gt100andlt115')
                                        ->where('bond_id', $gt100andlt115[0]['bond_id'])
                                        ->where('status', 1)
                                        ->setInc('add_ware');
                                    Db::table('gt100andlt115')
                                        ->where('bond_id', $gt100andlt115[0]['bond_id'])
                                        ->where('status', 1)
                                        ->update([
                                            'status' => 3,
                                        ]);
                                    // 提交事务
                                    Db::commit();

                                    break;
                                }
                            }
                        }
                    }
                }
            }

            //查看是否需要调仓
            $redeem = [];
            $gt100 = [];
            //先判断是否仓库是否有强赎的
            foreach ($gt100andlt115 as $key => $value) {
                if (in_array($value['bond_id'], $qiangshu)) {
                    $redeem[] = $value;
                }
            }

            if (count($redeem) > 0) {
                foreach ($redeem as $k => $v) {
                    if ($k == 1) {
                        break;
                    }
                    // 启动事务
                    Db::startTrans();
                    Db::table('gt100andlt115')
                        ->where('bond_id', $v['bond_id'])
                        ->where('status', 1)
                        ->update([
                            'status' => 4,
                        ]);
                    // 提交事务
                    Db::commit();
                }
            }

            $gt100andlt115 = Db::table('gt100andlt115')
                ->where('status', 1)
                ->select();

            //找出盈利大于100的并卖出
            foreach ($gt100andlt115 as $key => $value) {
                foreach ($finaldata as $vv) {
                    if ($value['bond_id'] == $vv['bond_id']) {
                        if (($vv['price'] - $value['price']) > 10) {
                            // 启动事务
                            Db::startTrans();
                            Db::table('gt100andlt115')
                                ->where('bond_id', $value['bond_id'])
                                ->where('status', 1)
                                ->update([
                                    'status' => 4,
                                ]);
                            // 提交事务
                            Db::commit();
                        } elseif (($value['price'] - $vv['price']) / $value['price'] > 0.05 && $value['add_ware'] < 3) {
                            // 启动事务
                            Db::startTrans();
                            Db::table('gt100andlt115')
                                ->where('bond_id', $value['bond_id'])
                                ->where('status', 1)
                                ->setInc('add_ware');
                            Db::table('gt100andlt115')
                                ->where('bond_id', $value['bond_id'])
                                ->where('status', 1)
                                ->update([
                                    'status' => 3,
                                ]);
                            // 提交事务 
                            Db::commit();
                        }
                    }
                }
                foreach ($update as $k => $v) {
                    if ($value['bond_id'] == $v['bond_id']) {
                        Db::table('gt100andlt115')
                            ->where('bond_id', $value['bond_id'])
                            ->where('status', 1)
                            ->update([
                                'earned' => ($v['price'] - $value['price']) * 10,
                                'time_price' => $v['price'],
                            ]);
                    }
                }
            }
            //发邮件
            //卖出数据拿出来
            $sellhose = Db::table('gt100andlt115')
                ->where('status', 4)
                ->select();

            //买入数据拿出来
            $buyhose = Db::table('gt100andlt115')
                ->where('status', 3)
                ->select();

            $sell = [];
            $buy = [];
            $inc = [];
            //换成邮件内容需要的数组
            foreach ($buyhose as $key => $value) { //买入
                if ($value['send_mail'] > 1) {
                    $inc[] = $value['id'];
                    $buy[] = '代码：' . $value['bond_id'] . ' 名字：' . $value['bond_name'] . '买入价格：' . $value['price'] . ' 类型：' . '盈利大于100,第' . $value['send_mail'] - 1 . '次加仓';
                } else {
                    $buy[] = '代码：' . $value['bond_id'] . ' 名字：' . $value['bond_name'] . '买入价格：' . $value['price'] . ' 类型：' . '盈利大于100';
                }
            }
            foreach ($sellhose as $key => $value) { //卖出
                $sell[] = '代码：' . $value['bond_id'] . ' 名字：' . $value['bond_name'] . ' 买入价格：' . $value['price'] . ' 卖出价格：' . $value['sell_price'] . ' 类型：' . '盈利大于100';
            }
            if (count($buy) > 0 || count($sell)) {
                $mailtext = '卖出:' . PHP_EOL . implode(PHP_EOL, $sell) . PHP_EOL . "买入" . PHP_EOL . implode(PHP_EOL, $buy);
                $res = email("盈利大于100", "调仓", '卖出' . count($sell) . "支买入" . count($buy) . "支明细：" . PHP_EOL . $mailtext);
                if ($res == 200) {
                    Db::startTrans();
                    Db::table('gt100andlt115')
                        ->where('status', 3)
                        ->update(['status' => 1]);
                    Db::table('gt100andlt115')
                        ->where('id', 'in', $inc)
                        ->setInc('send_mail');
                    Db::table('gt100andlt115')
                        ->where('status', 4)
                        ->update(['status' => 2, 'updated_at' => date('Y-m-d H:i:s')]);
                    Db::commit();
                    echo '盈利大于100调仓成功';
                }
            }
        } else {
            $insertData = [];
            //拿到高债底的数据
            foreach ($finaldata as $key => $value) {
                $insertData[] = $value;
            }

            //组装写入数据库数据
            foreach ($insertData as $key => $value) {
                $insertData[$key]['created_at'] = date('Y-m-d H:i:s');
            }
            $res = Db::table('gt100andlt115')
                ->insertAll($insertData);
            //换成邮件内容需要的数组
            foreach ($insertData as $key => $value) {
                $buy[] = '代码：' . $value['bond_id'] . ' 名字：' . $value['bond_name'] . ' 价格：' . $value['price'] . ' 类型：' . '盈利大于100';
            }
            $mailtext = "建仓:" . PHP_EOL . implode(PHP_EOL, $buy);
            $res = email("盈利大于100", "建仓", "共" . count($buy) . "支明细：" . PHP_EOL . $mailtext);
            if ($res == 200) {
                Db::startTrans();
                Db::table('gt100andlt115')
                    ->where('status', 3)
                    ->update(['status' => 1, 'send_mail' => 1]);
                Db::commit();

                echo '盈利大于100建仓成功';
            }
        }
    }

    /**
     * 象限3
     * @desc 处理象限3
     * 双低策略:双低值大于130，价格大于130均调仓
     * 想卖出双低值减去想买入的双低值大于8调仓
     */
    public
    function three($finaldata, $qiangshu)
    {

        //先排序价格最低到最高
        $dblow = array_column($finaldata, 'dblow');
        array_multisort($dblow, SORT_ASC, $finaldata);

        //获取仓库以外的
        $outside = $this->outside($finaldata, 2);
        $outsideprice = array_column($outside, 'dblow');
        array_multisort($outsideprice, SORT_ASC, $outside);

        //拿到仓库最新的
        $it_exist_three = Db::table('bonds_data')
            ->where('status', 'in', [1, 3])
            ->where('type', 2)
            ->select();

        if (count($it_exist_three) > 0) {

            //仓库不够10个才运行这里
            if (count($it_exist_three) < 10) {
                $outSideInsertData = [];
                foreach ($outside as $key => $value) {
                    if ($value['premium_rt'] < 15 && $value['price'] < 115) {
                        $outSideInsertData[] = $value;
                    }
                }
                if (count($outSideInsertData) > 0) {
                    for ($x = 0; $x < (10 - count($it_exist_three)); $x++) {
                        if (isset($outside[$x])) {
                            $ltInsertData[$x] = $outSideInsertData[$x];
                            $ltInsertData[$x]['type'] = 2;
                            $ltInsertData[$x]['created_at'] = date('Y-m-d H:i:s');
                            Db::table('bonds_data')->insert($ltInsertData[$x]);
                        } else {
                            break;
                        }
                    }
                }
            }


            //查看是否需要调仓
            $redeem = [];
            $pricegt130_dblowgt = [];
            //先判断是否仓库是否有强赎的
            foreach ($it_exist_three as $key => $value) {

                if (in_array($value['bond_id'], $qiangshu)) {
                    $redeem[] = $value;
                }
            }

            if (count($redeem) > 0) {
                foreach ($redeem as $k => $v) {
                    if ($k == 1) {
                        break;
                    }

                    // 启动事务
                    Db::startTrans();
                    Db::table('bonds_data')
                        ->where('bond_id', $v['bond_id'])
                        ->where('type', 2)
                        ->where('status', 1)
                        ->update([
                            'status' => 2,
                            'new_price' => $v['price'],
                        ]);

                    if (count($outside) > 0) {
                        $outside[$k]['type'] = 2;
                        $outside[$k]['created_at'] = date('Y-m-d H:i:s');
                        Db::table('bonds_data')
                            ->insert($outside[$k]);
                    }

                    // 提交事务
                    Db::commit();
                }
            }


            //拿到仓库最新的130系列用到
            $it_exist_three = Db::table('bonds_data')
                ->where('status', 'in', [1, 3])
                ->where('type', 2)
                ->select();

            //拿到仓库最新的并且更新数据
            foreach ($it_exist_three as $key => $value) {
                foreach ($finaldata as $vv) {
                    if ($value['bond_id'] == $vv['bond_id']) {
                        $it_exist_three[$key]['price'] = $vv['price'];
                        $it_exist_three[$key]['dblow'] = $vv['dblow'];
                        $it_exist_three[$key]['premium_rt'] = $vv['premium_rt'];
                        $it_exist_three[$key]['rating_cd'] = $vv['rating_cd'];
                    }
                }
            }

            //拿到仓库最新的双低值相差5用到
            $it_exist_three = Db::table('bonds_data')
                ->where('status', 'in', [1, 3])
                ->where('type', 2)
                ->select();

            //拿到仓库最新的并且更新数据
            foreach ($it_exist_three as $key => $value) {
                foreach ($finaldata as $vv) {
                    if ($value['bond_id'] == $vv['bond_id']) {
                        $it_exist_three[$key]['price'] = $vv['price'];
                        $it_exist_three[$key]['dblow'] = $vv['dblow'];
                        $it_exist_three[$key]['premium_rt'] = $vv['premium_rt'];
                        $it_exist_three[$key]['rating_cd'] = $vv['rating_cd'];
                    }
                }
            }

            //获取仓库以外的
            $outside = $this->outside($finaldata, 2);
            $outsideprice = array_column($outside, 'dblow');
            array_multisort($outsideprice, SORT_ASC, $outside);
            //先排序价格最低到最高
            $dblow = array_column($it_exist_three, 'dblow');
            array_multisort($dblow, SORT_DESC, $it_exist_three);

            //调出双低减去调入双低>5或者双低值大于130，价格大于130均调仓
            foreach ($it_exist_three as $key => $value) {
                if ($key == 1) {
                    break;
                }
                $outSideUpdateData = [];

                foreach ($outside as $keys => $values) {
                    if ($values['premium_rt'] < 15 && $values['price'] < 115) {
                        $outSideUpdateData[] = $values;
                    }
                }

                if (count($outSideUpdateData) > 0) {
                    $outSideUpdateData = array_values($this->array_unique_fb($outSideUpdateData));
                    if (($value['time_dblow'] - $outSideUpdateData[$key]['dblow']) > 5 || $value['time_price'] > 130 || $value['time_dblow'] > 130) {
                        $outSideUpdateData[$key]['type'] = 2;
                        $outSideUpdateData[$key]['created_at'] = date('Y-m-d H:i:s');
                        // 启动事务
                        Db::startTrans();
                        Db::table('bonds_data')
                            ->where('bond_id', $value['bond_id'])
                            ->where('type', 2)
                            ->where('status', 1)
                            ->update([
                                'status' => 2,
                                'new_price' => $value['time_price'],
                            ]);
                        Db::table('bonds_data')
                            ->insert($outSideUpdateData[$key]);

                        // 提交事务
                        Db::commit();
                    }
                }
            }
        } else {
            $insertData = [];
            //拿到双低的数据
            foreach ($finaldata as $key => $value) {
                if ($value['premium_rt'] < 15 && $value['price'] < 115) {
                    $insertData[] = $value;
                }
                if (count($insertData) == 10) {
                    break;
                }
            }

            //组装写入数据库数据
            foreach ($insertData as &$value) {
                $value['type'] = 2;
                $value['created_at'] = date('Y-m-d H:i:s');
            }
            $res = Db::table('bonds_data')
                ->insertAll($insertData);
        }
    }

    /**
     * 象限4
     * @desc 处理象限4
     *低溢价策略，想卖出溢价率减去想买入溢价率大于8调仓
     */
    public
    function four($finaldata, $qiangshu)
    {
        $finaldata = $this->sort($finaldata);
        //获取仓库以外的
        $outside = $this->outside($finaldata, 3);
        $clone = $outside = $this->sort($outside);
        $outsideData = [];
        foreach ($clone as $key => $value) {
            if ($value['price'] < 130) {
                $outsideData[] = $value;
            }
        }
        $outside = [];
        $outside = $outsideData;
        //拿到仓库最新的
        $it_exist_four = Db::table('bonds_data')
            ->where('status', 'in', [1, 3])
            ->where('type', 3)
            ->select();

        if (count($it_exist_four) > 0) {
            foreach ($clone as $key => $value) {
                if ($value['price'] < 130) {
                    $outsideData[] = $value;
                }
            }
            $outside = [];
            $outside = $outsideData;
            //仓库不够10个才运行这里
            if (count($it_exist_four) < 10) {

                if (count($outside) > 0) {
                    for ($x = 0; $x < (10 - count($it_exist_four)); $x++) {

                        if (isset($outside[$x])) {
                            unset($outside[$x]['sort']);
                            $ltInsertData[$x] = $outside[$x];
                            $ltInsertData[$x]['type'] = 3;
                            $ltInsertData[$x]['created_at'] = date('Y-m-d H:i:s');
                            Db::table('bonds_data')
                                ->insert($ltInsertData[$x]);
                        } else {
                            break;
                        }
                    }
                }
            }

            //获取仓库以外的
            $outside = $this->outside($finaldata, 3);
            $clone = $outside = $this->sort($outside);
            $outsideData = [];
            foreach ($clone as $key => $value) {
                if ($value['price'] < 130) {
                    $outsideData[] = $value;
                }
            }
            $outside = [];
            $outside = $outsideData;
            //查看是否需要调仓
            $redeem = [];
            //先判断是否仓库是否有强赎的
            foreach ($it_exist_four as $key => $value) {

                if (in_array($value['bond_id'], $qiangshu)) {
                    $redeem[] = $value;
                }
            }

            if (count($redeem) > 0) {
                foreach ($redeem as $k => $v) {
                    if ($k == 1) {
                        break;
                    }
                    // 启动事务
                    Db::startTrans();

                    Db::table('bonds_data')
                        ->where('bond_id', $v['bond_id'])
                        ->where('status', 1)
                        ->where('type', 3)
                        ->update(['status' => 2, 'new_price' => $v['price']]);

                    if (count($outside) > 0) {
                        unset($outside[$k]['sort']);
                        $outside[$k]['type'] = 3;
                        $outside[$k]['created_at'] = date('Y-m-d H:i:s');
                        Db::table('bonds_data')
                            ->insert($outside[$k]);
                    }

                    // 提交事务
                    Db::commit();
                }
            }


            //拿到仓库最新的用于卖出价格涨到特别高但是溢价率还是很低的债
            $it_exist_four = Db::table('bonds_data')
                ->where('status', 'in', [1, 3])
                ->where('type', 3)
                ->select();

            //获取仓库以外的
            $outside = $this->outside($finaldata, 3);
            $clone = $outside = $this->sort($outside);
            $outsideData = [];
            foreach ($clone as $key => $value) {
                if ($value['price'] < 130) {
                    $outsideData[] = $value;
                }
            }
            $outside = [];
            $outside = $outsideData;
            $it_exist_four = $this->asort($it_exist_four);
            //查看有没有溢价率大于非仓库5
            foreach ($it_exist_four as $k => $v) {
                if ($k == 1) {
                    break;
                }
                if (($v['time_price'] - $v['price']) / $v['price'] > 0.3) {
                    unset($v['sort']);
                    unset($outside[$k]['sort']);
                    $outside[$k]['type'] = 3;
                    $outside[$k]['created_at'] = date('Y-m-d H:i:s');
                    // 启动事务
                    Db::startTrans();
                    Db::table('bonds_data')
                        ->where('bond_id', $v['bond_id'])
                        ->where('status', 1)
                        ->where('type', 3)
                        ->update([
                            'status' => 2,
                            'new_price' => $v['time_price'],
                        ]);

                    Db::table('bonds_data')
                        ->insert($outside[$k]);

                    // 提交事务
                    Db::commit();
                }
            }


            //拿到仓库最新的
            $it_exist_four = Db::table('bonds_data')
                ->where('status', 'in', [1, 3])
                ->where('type', 3)
                ->select();
            //拿到仓库最新的,更新数据
            foreach ($it_exist_four as $key => $value) {
                foreach ($finaldata as $vv) {
                    if ($value['bond_id'] == $vv['bond_id']) {
                        $it_exist_four[$key]['price'] = $vv['price'];
                        $it_exist_four[$key]['dblow'] = $vv['dblow'];
                        $it_exist_four[$key]['premium_rt'] = $vv['premium_rt'];
                        $it_exist_four[$key]['rating_cd'] = $vv['rating_cd'];
                    }
                }
            }


            //获取仓库以外的
            $outside = $this->outside($finaldata, 3);
            $clone = $outside = $this->sort($outside);
            $outsideData = [];
            foreach ($clone as $key => $value) {
                if ($value['price'] < 130) {
                    $outsideData[] = $value;
                }
            }
            $outside = [];
            $outside = $outsideData;
            $it_exist_four = $this->asort($it_exist_four);
            //查看有没有溢价率大于非仓库5
            foreach ($it_exist_four as $k => $v) {
                if ($k == 1) {
                    break;
                }
                if (($v['time_premium_rt'] - $outside[$k]['premium_rt']) > 5) {
                    unset($v['sort']);
                    unset($outside[$k]['sort']);
                    $outside[$k]['type'] = 3;
                    $outside[$k]['created_at'] = date('Y-m-d H:i:s');
                    // 启动事务
                    Db::startTrans();
                    Db::table('bonds_data')
                        ->where('bond_id', $v['bond_id'])
                        ->where('status', 1)
                        ->where('type', 3)
                        ->update([
                            'status' => 2,
                            'new_price' => $v['price'],
                        ]);

                    Db::table('bonds_data')
                        ->insert($outside[$k]);

                    // 提交事务
                    Db::commit();
                }
            }
        } else {
            $insertData = [];
            //拿到低溢价的数据
            foreach ($finaldata as $key => $value) {
                unset($value['sort']);
                if ($value['price'] < 130) {
                    $insertData[] = $value;
                } else {
                    if ($value['premium_rt'] < -3 && $value['price'] > 130) {
                        $insertData[] = $value;
                    }
                }

                if (count($insertData) == 10) {
                    break;
                }
            }
            //组装写入数据库数据
            foreach ($insertData as &$value) {
                $value['type'] = 3;
                $value['created_at'] = date('Y-m-d H:i:s');
            }
            $res = Db::table('bonds_data')
                ->insertAll($insertData);
        }
    }


    /**
     * 价格和溢价率
     * @desc
     *溢价率小于40且价格105内，溢价率小于30且价格110内，溢价率小于20且价格115内，溢价率小于10且价格120内，溢价率小于0且价格130内
     * 卖出策略价格涨幅30%或者溢价率涨幅30%
     */
    public
    function tan($finaldata, $qiangshu)
    {
        $finaldata = $this->sort($finaldata);
        //获取仓库以外的
        $outside = $this->outside($finaldata, 10);
        $clone = $outside = $this->sort($outside);
        $outsideData = [];
        foreach ($clone as $key => $value) {
            //            if ($value['curr_iss_amt'] < 4) {
            if (
                $value['price'] <= 100 && $value['premium_rt'] <= 40 ||
                $value['price'] <= 110 && $value['premium_rt'] <= 30 ||
                $value['price'] <= 120 && $value['premium_rt'] <= 20 ||
                $value['price'] <= 130 && $value['premium_rt'] <= -5 ||
                $value['price'] <= 140 && $value['premium_rt'] <= -10
            ) {
                $outsideData[] = $value;
            }
            //            }
        }
        $outside = [];
        $outside = $outsideData;
        //拿到仓库最新的
        $it_exist_four = Db::table('bonds_data')
            ->where('status', 'in', [1, 3])
            ->where('type', 10)
            ->select();

        if (count($it_exist_four) > 0) {
            //仓库不够30个才运行这里
            //            if (count($it_exist_four) < 30) {
            //
            //                if (count($outside) > 0) {
            //                    for ($x = 0; $x < (30 - count($it_exist_four)); $x++) {
            //                        if (isset($outside[$x])) {
            //                            unset($outside[$x]['sort']);
            //                            $ltInsertData[$x] = $outside[$x];
            //                            $ltInsertData[$x]['type'] = 10;
            //                            $ltInsertData[$x]['created_at'] = date('Y-m-d H:i:s');
            //                            Db::table('bonds_data')
            //                                ->insert($ltInsertData[$x]);
            //                        } else {
            //                            break;
            //                        }
            //                    }
            //                }
            //            }

            //获取仓库以外的
            $outside = $this->outside($finaldata, 10);
            $clone = $outside = $this->sort($outside);
            $outsideData = [];
            foreach ($clone as $key => $value) {
                //                if ($value['curr_iss_amt'] < 4) {
                if (
                    $value['price'] <= 100 && $value['premium_rt'] <= 40 ||
                    $value['price'] <= 110 && $value['premium_rt'] <= 30 ||
                    $value['price'] <= 120 && $value['premium_rt'] <= 20 ||
                    $value['price'] <= 130 && $value['premium_rt'] <= -5 ||
                    $value['price'] <= 140 && $value['premium_rt'] <= -10
                ) {
                    $outsideData[] = $value;
                }
                //                }
            }
            $outside = [];
            $outside = $outsideData;
            //查看是否需要调仓
            $redeem = [];
            //先判断是否仓库是否有强赎的
            foreach ($it_exist_four as $key => $value) {

                if (in_array($value['bond_id'], $qiangshu)) {
                    $redeem[] = $value;
                }
            }

            if (count($redeem) > 0) {
                foreach ($redeem as $k => $v) {
                    if ($k == 1) {
                        break;
                    }
                    // 启动事务
                    Db::startTrans();

                    Db::table('bonds_data')
                        ->where('bond_id', $v['bond_id'])
                        ->where('status', 1)
                        ->where('type', 10)
                        ->update(['status' => 2, 'new_price' => $v['price']]);

                    if (count($outside) > 0) {
                        unset($outside[$k]['sort']);
                        $outside[$k]['type'] = 10;
                        $outside[$k]['created_at'] = date('Y-m-d H:i:s');
                        Db::table('bonds_data')
                            ->insert($outside[$k]);
                    }
                    // 提交事务
                    Db::commit();
                }
            }

            //拿到仓库最新的
            $it_exist_four = Db::table('bonds_data')
                ->where('status', 'in', [1, 3])
                ->where('type', 10)
                ->select();

            //获取仓库以外的
            $outside = $this->outside($finaldata, 10);
            $clone = $outside = $this->sort($outside);
            $outsideData = [];
            foreach ($clone as $key => $value) {
                //                if ($value['curr_iss_amt'] < 4) {
                if (
                    $value['price'] <= 100 && $value['premium_rt'] <= 40 ||
                    $value['price'] <= 110 && $value['premium_rt'] <= 30 ||
                    $value['price'] <= 120 && $value['premium_rt'] <= 20 ||
                    $value['price'] <= 130 && $value['premium_rt'] <= -5 ||
                    $value['price'] <= 140 && $value['premium_rt'] <= -10
                ) {
                    $outsideData[] = $value;
                }
                //                }
            }
            $outside = [];
            $outside = $outsideData;
            $it_exist_four = $this->asort($it_exist_four);
            //查看有没有溢价率大于60或者价格涨幅大于30%
            foreach ($it_exist_four as $k => $v) {
                if ($k == 1) {
                    break;
                }
                if (($v['time_price'] - $v['price']) / $v['price'] > 0.3 || $v['curr_iss_amt'] > 2 && $v['time_premium_rt'] > 60) {
                    unset($v['sort']);
                    unset($outside[$k]['sort']);
                    $outside[$k]['type'] = 10;
                    $outside[$k]['created_at'] = date('Y-m-d H:i:s');
                    // 启动事务
                    Db::startTrans();
                    Db::table('bonds_data')
                        ->where('bond_id', $v['bond_id'])
                        ->where('status', 1)
                        ->where('type', 10)
                        ->update([
                            'status' => 2,
                            'new_price' => $v['time_price'],
                        ]);

                    Db::table('bonds_data')
                        ->insert($outside[$k]);

                    // 提交事务
                    Db::commit();
                    //加仓标记为0就是没加过，每次加仓需要判断是不是比标记要大，如果大就加，小于的话就是没达到加仓要求，最多仓位5%，避免重仓一个
                } elseif (($v['price'] - $v['time_price']) / $v['price'] > 0.05 && ($v['price'] - $v['time_price']) / $v['price'] < 0.1) {
                    $add_ware = Db::table('bonds_data')
                        ->where('status', 1)
                        ->where('type', 10)
                        ->where('bond_id', $v['bond_id'])
                        ->value('add_ware');
                    if ($add_ware < 5) {
                        $res = email("鲨鱼式摊大饼", "跌5%", '买入1仓位' . $v['bond_name'] . '(' . $v['bond_id'] . ')');
                        if ($res == 200) {
                            Db::startTrans();
                            Db::table('bonds_data')
                                ->where('bond_id', $v['bond_id'])
                                ->where('status', 1)
                                ->where('type', 10)
                                ->update([
                                    'add_ware' => 5,
                                ]);
                            Db::commit();
                            echo '摊大饼调仓成功';
                        }
                    }
                } elseif (($v['price'] - $v['time_price']) / $v['price'] > 0.1 && ($v['price'] - $v['time_price']) / $v['price'] < 0.15) {
                    $add_ware = Db::table('bonds_data')
                        ->where('status', 1)
                        ->where('type', 10)
                        ->where('bond_id', $v['bond_id'])
                        ->value('add_ware');
                    if ($add_ware == 5) {
                        $res = email("鲨鱼式摊大饼", "跌10%", '买入2仓位' . $v['bond_name'] . '(' . $v['bond_id'] . ')');
                        if ($res == 200) {
                            Db::startTrans();
                            Db::table('bonds_data')
                                ->where('bond_id', $v['bond_id'])
                                ->where('status', 1)
                                ->where('type', 10)
                                ->update([
                                    'add_ware' => 10,
                                ]);
                            Db::commit();
                            echo '摊大饼调仓成功';
                        }
                    }
                } elseif (($v['price'] - $v['time_price']) / $v['price'] > 0.15) {
                    $add_ware = Db::table('bonds_data')
                        ->where('status', 1)
                        ->where('type', 10)
                        ->where('bond_id', $v['bond_id'])
                        ->value('add_ware');
                    if ($add_ware == 10) {
                        $res = email("鲨鱼式摊大饼", "跌15%", '买入3仓位' . $v['bond_name'] . '(' . $v['bond_id'] . ')');
                        if ($res == 200) {
                            Db::startTrans();
                            Db::table('bonds_data')
                                ->where('bond_id', $v['bond_id'])
                                ->where('status', 1)
                                ->where('type', 10)
                                ->update([
                                    'add_ware' => 10,
                                ]);
                            Db::commit();
                            echo '摊大饼调仓成功';
                        }
                    }
                }
            }
        } else { //溢价率小于40且价格105内，溢价率小于30且价格110内，溢价率小于20且价格115内，溢价率小于10且价格120内，溢价率小于0且价格130内
            $insertData = [];
            //拿到低溢价的数据
            $i = 0;
            foreach ($finaldata as $key => $value) {
                //                if ($i == 30) {
                //                    break;
                //                }
                //                if ($value['curr_iss_amt'] < 4) {
                if (
                    $value['price'] <= 100 && $value['premium_rt'] <= 40 ||
                    $value['price'] <= 110 && $value['premium_rt'] <= 30 ||
                    $value['price'] <= 120 && $value['premium_rt'] <= 20 ||
                    $value['price'] <= 130 && $value['premium_rt'] <= -5 ||
                    $value['price'] <= 140 && $value['premium_rt'] <= -10
                ) {
                    unset($value['sort']);
                    $insertData[] = $value;
                    $i++;
                }
                //                }
            }
            //            dump($insertData);
            //            die;
            //组装写入数据库数据
            foreach ($insertData as &$value) {
                $value['type'] = 10;
                $value['created_at'] = date('Y-m-d H:i:s');
            }
            $res = Db::table('bonds_data')
                ->insertAll($insertData);
        }
    }

    /**
     * 三低可转债简单来说就是基于双低：低价格、低溢价的基础上，增加了余额低。
     * @desc
     *  低价格：我认为是价格低于125以下的可转债，价格相对双低会高一些。
     * 低溢价：溢价相对比较低，我认为是溢价低于50%的，相对双低也会高一些。
     * 余额低：很多可转债达到转股后，可转债会很多转股，剩下余额低于2.5亿的。
     *
     * 公式：三低值 = 可转债当前价格*（1+溢价率）*可转债余额  （三低值越低表示性价比越高，三低值越高，表示性价比越低）
     *
     * 以后有时间再开发或者人工操作
     * 三低策略升级
     *  1、属于深市：涨跌幅度的限制，之前深市涨跌幅限制很宽松，当达到30%的涨幅停牌半小时后，涨跌幅限制取消，这种转债更容易被炒作。就是因为炒作太多，后来深市学习沪市修改了规则。相对于沪市，没那么宽松了。但很多以前被爆炒的可转债都是深圳的。
     * 2、具有妖性：对于低规模的，首选被爆炒过的，特别是多次爆炒的，只有这种才更具有妖性，更容易被资本市场再次爆炒。如万顺转债、三星转债、正欲转债。
     * 3、热点：对于一些短期的热点转债，如思特转债，从很低的价格，忽然因为鸿蒙的概念炒作，第二天涨幅超过了40%，涨幅惊人。另外，对于名人林园持仓的也可以考虑，这些都是热点，你抓到就可以博取更高收益。
     *
     */
    public
    function threeLow($finaldata, $qiangshu)
    {
        //三低值计算
        $threeLowData = [];
        foreach ($finaldata as $value) {
            if (
                $value['price'] < 125 &&
                $value['premium_rt'] < 50 &&
                $value['curr_iss_amt'] < 2.5
            ) {
                $value['three_low'] = $value['price'] * (1 + $value['premium_rt']) * $value['curr_iss_amt'];
                $threeLowData[] = $value;
            }
        }
        //三低值从小到大排序
        $last_names = array_column($threeLowData, 'three_low');
        array_multisort($last_names, SORT_ASC, $threeLowData);

        //获取仓库以外的
        $clone = $outside = $this->outside($finaldata, 30);
        $outsideData = [];
        foreach ($clone as $key => $value) {
            if (
                $value['price'] < 125 &&
                $value['premium_rt'] < 50 &&
                $value['curr_iss_amt'] < 2.5
            ) {
                $value['three_low'] = $value['price'] * (1 + $value['premium_rt']) * $value['curr_iss_amt'];
                $outsideData[] = $value;
            }
        }
        $outside = $outsideData;
        //拿到仓库最新的
        $it_exist_four = Db::table('bonds_data')
            ->where('status', 'in', [1, 3])
            ->where('type', 30)
            ->select();

        if (count($it_exist_four) > 0) {
            //仓库不够20个才运行这里
            if (count($it_exist_four) < 20) {

                if (count($outside) > 0) {
                    for ($x = 0; $x < (20 - count($it_exist_four)); $x++) {
                        if (isset($outside[$x])) {
                            $ltInsertData30[$x] = $outside[$x];
                            $ltInsertData30[$x]['type'] = 30;
                            $ltInsertData30[$x]['created_at'] = date('Y-m-d H:i:s');
                            Db::table('bonds_data')
                                ->insert($ltInsertData30[$x]);
                        } else {
                            break;
                        }
                    }
                }
            }

            //查看是否需要调仓
            $redeem = [];
            //先判断是否仓库是否有强赎的
            foreach ($it_exist_four as $key => $value) {

                if (in_array($value['bond_id'], $qiangshu)) {
                    $redeem[] = $value;
                }
            }

            if (count($redeem) > 0) {
                foreach ($redeem as $k => $v) {
                    if ($k == 1) {
                        break;
                    }
                    // 启动事务
                    Db::startTrans();

                    Db::table('bonds_data')
                        ->where('bond_id', $v['bond_id'])
                        ->where('status', 1)
                        ->where('type', 30)
                        ->update(['status' => 2, 'new_price' => $v['price']]);
                    // 提交事务
                    Db::commit();
                }
            }

            //拿到仓库最新的
            $it_exist_four = Db::table('bonds_data')
                ->where('status', 'in', [1, 3])
                ->where('type', 30)
                ->select();

            //获取仓库以外的
            $clone = $outside = $this->outside($finaldata, 30);
            $outsideData = [];
            foreach ($clone as $key => $value) {
                if (
                    $value['price'] < 125 &&
                    $value['premium_rt'] < 50 &&
                    $value['curr_iss_amt'] < 2.5
                ) {
                    $value['three_low'] = $value['price'] * (1 + $value['premium_rt']) * $value['curr_iss_amt'];
                    $outsideData[] = $value;
                }
            }
            $outside = $outsideData;
            $it_exist_four = $this->asort($it_exist_four);
            //查看有没有溢价率大于60或者价格涨幅大于30%
            foreach ($it_exist_four as $k => $v) {

                if (($v['time_price'] - $v['price']) / $v['price'] > 0.3 || $v['premium_rt'] * 3 < $v['time_premium_rt']) {
                    $outside[$k]['type'] = 30;
                    $outside[$k]['created_at'] = date('Y-m-d H:i:s');
                    // 启动事务
                    Db::startTrans();
                    Db::table('bonds_data')
                        ->where('bond_id', $v['bond_id'])
                        ->where('status', 1)
                        ->where('type', 30)
                        ->update([
                            'status' => 2,
                            'new_price' => $v['time_price'],
                        ]);
                    Db::commit();
                }
            }
        } else {
            $i = 0;
            foreach ($threeLowData as $key => $value) {
                if ($i >= 20) {
                    break;
                }
                $value['type'] = 30;
                $value['created_at'] = date('Y-m-d H:i:s');
                Db::table('bonds_data')->insert($value);
            }
        }
    }


    //剩余规模排序从小到大
    public
    function sort($finaldata)
    {
        $dowmyijia = [];
        foreach ($finaldata as $key => $value) {
            $value['sort'] = (float)$value['curr_iss_amt'];
            $dowmyijia[] = $value;
        }
        //负溢价率从小到大排序
        $last_names = array_column($dowmyijia, 'sort');
        array_multisort($last_names, SORT_ASC, $dowmyijia);
        return $dowmyijia;
    }

    //溢价率排序从小到大只算负的
    public
    function sortPrice($finaldata)
    {
        $dowmyijia = [];
        foreach ($finaldata as $key => $value) {
            $value['sort'] = (float)$value['price_rate'];
            $dowmyijia[] = $value;
        }
        //负溢价率从小到大排序
        $last_names = array_column($dowmyijia, 'sort');
        array_multisort($last_names, SORT_DESC, $dowmyijia);
        return $dowmyijia;
    }

    //溢价率排序从小到大
    public
    function sortRealPrice($finaldata)
    {
        $last_names = array_column($finaldata, 'premium_rt');
        array_multisort($last_names, SORT_ASC, $finaldata);
        $lessThan0 = [];
        $equalTo0 = [];
        $greaterThan0 = [];

        foreach ($finaldata as $key => $value) {
            if ($value['premium_rt'] < 0) {
                $lessThan0[] = $value;
            } elseif ($value['premium_rt'] > 0) {
                $greaterThan0[] = $value;
            } else {
                $equalTo0[] = $value;
            }
        }
        //负溢价率从小到大排序
        return array_merge($lessThan0, $equalTo0, $greaterThan0);
    }

    //溢价率排序从大到小
    public
    function asort($finaldata)
    {
        $dowmyijia = [];
        foreach ($finaldata as $key => $value) {
            $value['sort'] = (float)$value['premium_rt'];
            $dowmyijia[] = $value;
        }
        //负溢价率从小到大排序
        $last_names = array_column($dowmyijia, 'sort');
        array_multisort($last_names, SORT_DESC, $dowmyijia);
        return $dowmyijia;
    }

    //拿出高评级的AA+ $finaldata
    public
    function highGrade($finaldata)
    {
        $grade = [];
        foreach ($finaldata as $value) {
            if ($value['rating_cd'] == 'AA+' || $value['rating_cd'] == 'AAA') {
                $grade[] = $value;
            }
        }

        return $grade;
    }


    //折价率排序从小到大
    public
    function price_rate_asort($finaldata)
    {
        $dowmyijia = [];
        foreach ($finaldata as $key => $value) {
            $value['sort'] = (float)$value['price_rate'];
            $dowmyijia[] = $value;
        }
        //负溢价率从小到大排序
        $last_names = array_column($dowmyijia, 'sort');
        array_multisort($last_names, SORT_ASC, $dowmyijia);
        return $dowmyijia;
    }

    //折价率排序从小到大
    public
    function price_rate_dsort($finaldata)
    {
        $dowmyijia = [];
        foreach ($finaldata as $key => $value) {
            $value['sort'] = (float)$value['price_rate'];
            $dowmyijia[] = $value;
        }
        //负溢价率从小到大排序
        $last_names = array_column($dowmyijia, 'sort');
        array_multisort($last_names, SORT_DESC, $dowmyijia);
        return $dowmyijia;
    }


    public
    function sell_price($data)
    {
        //定义每份是都是 暂定5% 10w 5 1w就是0.5
        $each = 5;
        //计算折价率（其实即使溢价率的负数值的绝对值）
        foreach ($data as $key => $value) {
            unset($value['dblow']);
            if ($value['premium_rt'] < 0) {
                $data[$key]['price_rate'] = abs($value['premium_rt']) * 100;
            } else {
                unset($data[$key]);
            }
        }

        //折价率从大到小排序
        $data = $this->sortPrice($data);
        $insertData = [];
        $sum = 100;
        foreach ($data as $key => $value) {
            unset($value['sort']);
            if ($value['price_rate'] > 6) {
                if ($sum >= 30) {
                    $value['position'] = 30;
                    $value['differ'] = 30;
                    $value['status'] = 1;
                    $value['created_at'] = date('Y-m-d H:i:s');
                    $sum -= 30;
                    $insertData[] = $value;
                }
            } else {
                if ($sum >= 5) {
                    $value['position'] = floor($value['price_rate']) * $each;
                    $value['differ'] = floor($value['price_rate']) * $each;
                    $value['status'] = 1;
                    $value['created_at'] = date('Y-m-d H:i:s');
                    $sum -= ceil($value['price_rate']) * $each;
                    $insertData[] = $value;
                }
            }
        }

        //拿到仓库最新的
        $it_exist_sell_price = Db::table('sell_price')
            ->where('status', 1)
            ->select();

        $insert_data_bond_ids = array_column($insertData, 'bond_id');
        $it_exist_bond_ids = array_column($it_exist_sell_price, 'bond_id');


        $sellbondis = array_diff($it_exist_bond_ids, $insert_data_bond_ids);
        if (count($it_exist_sell_price) > 0) {
            $selldiffer = 0;
            $sell = [];
            //处理卖出的
            foreach ($it_exist_sell_price as $key => $value) {

                if (in_array($value['bond_id'], $sellbondis)) {
                    Db::table('sell_price')
                        ->where('status', 1)
                        ->where('bond_id', $value['bond_id'])
                        ->update([
                            'status' => 2,
                            'is_del_time' => time(),
                        ]);
                    $selldiffer += $value['position'];
                    //卖出去的要写进新增的，这个处理方法是每次调仓都会增加数据，把旧的都删了
                    $value['differ'] = 0 - $value['position'];
                    $value['is_del_time'] = time();
                    unset($value['id']);
                    $sell[] = $value;
                }
            }
            //拿到仓库最新的
            $it_exist_sell_price = Db::table('sell_price')
                ->where('status', 1)
                ->select();
            $it_exist_bond_ids = array_column($it_exist_sell_price, 'bond_id');
            $insertData = array_values($insertData);

            foreach ($insertData as $key => $value) {
                $insertData[$key]['is_del_time'] = time();
                if (in_array($value['bond_id'], $it_exist_bond_ids)) {
                    $it_exist_key_bound_id = array_column($it_exist_sell_price, null, 'bond_id');
                    if ($value['position'] == $it_exist_key_bound_id[$value['bond_id']]['position']) { //判断仓位是否相同，相同就不调
                        $insertData[$key]['differ'] = 0;
                    } else {
                        $insertData[$key]['differ'] = $value['position'] - $it_exist_key_bound_id[$value['bond_id']]['position'];
                        if ($insertData[$key]['differ'] > 0) {
                            if (round($value['sincrease_rt'] == -10) || round($value['sincrease_rt'] == -20)) {
                                $insertData[$key]['differ'] = 0;
                            }
                        } else {
                            if (round($value['sincrease_rt'] == 10) || round($value['sincrease_rt'] == 20)) {
                                $insertData[$key]['differ'] = 0;
                            }
                        }
                        Db::table('sell_price')
                            ->where('status', 1)
                            ->where('bond_id', $value['bond_id'])
                            ->update([
                                'status' => 2,
                                'is_del_time' => time(),
                            ]);
                        if ($insertData[$key]['differ'] < 0) {
                            $selldiffer += abs($insertData[$key]['differ']);
                        }
                    }
                } else {
                    if (round($value['sincrease_rt'] == -10) || round($value['sincrease_rt'] == -20)) {
                        $insertData[$key]['position'] = 0;
                    }
                    $insertData[$key]['differ'] = $insertData[$key]['position'];
                }
            }

            $sum = array_sum(array_column($insertData, 'differ'));
            if (empty($sum)) {
                return 0;
            }
            Db::table('sell_price')->where('status', 1)
                ->update([
                    'status' => 2,
                ]);
            Db::table('sell_price')->insertAll($insertData);
            if (count($sell) > 0) {
                Db::table('sell_price')->insertAll($sell);
            }
        } else {
            $totaldata = array_column($insertData, 'position');
            $total = array_sum($totaldata);

            $totaldiffer = array_column($insertData, 'differ');
            $differ = array_sum($totaldiffer);

            if ($total > 100) {
                array_pop($totaldata);
                $end = 100 - array_sum($totaldata);
                $insertData[count($totaldata)]['position'] = $end;
            }
            if ($differ > 100) {
                array_pop($totaldiffer);
                $end = 100 - array_sum($totaldiffer);
                $insertData[count($totaldiffer)]['differ'] = $end;
            }
            Db::table('sell_price')->insertAll($insertData);
        }

        //发邮件

        //变动的数据拿出来
        $sell_price_hose = Db::table('sell_price')
            ->where('status', 1)
            ->select();

        //去重
        $sell_price_hose = $this->array_unique_fb($sell_price_hose);

        $sell = [];
        $buy = [];
        $not = [];
        if (empty($sell_price_hose)) {
            echo '折价没达到调仓条件';
        } else {
            //换成邮件内容需要的数组
            foreach ($sell_price_hose as $key => $value) {
                if ($value['differ'] > 0) {
                    $buy[] = '代码：' . $value['bond_id'] . ' 名字：' . $value['bond_name'] . '买入价格：' . $value['price'] . '买入数量：' . $value['differ'] . ' 正股涨跌：' . $value['sincrease_rt'];
                } elseif ($value['differ'] < 0) {
                    $sell[] = '代码：' . $value['bond_id'] . ' 名字：' . $value['bond_name'] . ' 买入价格：' . $value['price'] . ' 卖出价格：' . $value['sell_price'] . '卖出数量：' . $value['differ'] . ' 正股涨跌：' . $value['sincrease_rt'];
                } else {
                    $not[] = '代码：' . $value['bond_id'] . ' 名字：' . $value['bond_name'] . '买入价格：' . $value['price'] . '买入数量：' . $value['differ'] . ' 正股涨跌：' . $value['sincrease_rt'];
                }
            }
            $mailtext = '卖出:' . PHP_EOL . implode(PHP_EOL, $sell) . PHP_EOL . "买入:" . PHP_EOL . implode(PHP_EOL, $buy) . PHP_EOL . "不动:" . PHP_EOL . implode(PHP_EOL, $not);
            $res = email("折价率套现", "调仓", '卖出' . count($sell) . "支买入" . count($buy) . "不动" . count($not) . "支明细：" . PHP_EOL . $mailtext);
            if ($res == 200) {

                Db::table('sell_price')
                    ->where('status', 2)
                    ->where('is_del_time', 'elt', time() - 1209600)
                    ->delete();

                Db::table('group')
                    ->where('group_id', 2)
                    ->update([
                        'is_one' => 1,
                    ]);

                Db::table('mail_log')
                    ->insert([
                        'title' => '折价组合',
                        'content' => $mailtext,
                        'buy' => count($buy),
                        'sell' => count($sell),
                        'create_at' => date('Y-m-d H:i:s'),
                    ]);
                echo '折价组合调仓成功';
            }
        }
    }

    public
    function low_premium_rt($data, $qiangshu)
    {

        $data10 = [];
        foreach ($data as $key => $value) {
            if ($key > 9) {
                break;
            } else {
                $data10[] = $value;
            }
        }
        $data = array_values($data10);

        //折价率从小到大排序
        $last_names = array_column($data, 'premium_rt');
        array_multisort($last_names, SORT_ASC, $data);


        //小于170
        $five = [];
        foreach ($data as $key => $value) {
            if ($value['price'] < 170) {
                $five[] = $value;
            }
        }


        //拿到仓库最新的
        $it_five = Db::table('dblow_premium')
            ->where('status', 1)
            ->select();

        if (count($it_five) > 0) {
            //排名跌出前10卖出
            $it_exist_bond_ids = array_column($it_five, 'bond_id');
            $tendata_bond_ids = array_column($data, 'bond_id');
            $time_premium_rt = array_column($it_five, 'time_price', 'bond_id');
            foreach ($it_exist_bond_ids as $bond_id) {
                if (!in_array($bond_id, $tendata_bond_ids)) {
                    Db::table('dblow_premium')
                        ->where('bond_id', $bond_id)
                        ->where('status', 1)
                        ->update(['status' => 2, 'new_price' => $time_premium_rt[$bond_id]]);
                }
            }


            //再次拿到仓库最新的看看还剩几个没卖
            $it_exist_sell_price = Db::table('dblow_premium')
                ->where('status', 1)
                ->select();
            if (count($it_exist_sell_price) > 0) {
                $it_exist_bond_ids = array_column($it_exist_sell_price, 'bond_id');
                $Data = [];
                foreach ($five as $key => $value) {
                    if (!in_array($value['bond_id'], $it_exist_bond_ids)) {
                        $Data[] = $value;
                    }
                }
                $insertData = [];
                for ($x = 0; $x < 10 - count($it_exist_sell_price); $x++) {
                    if (!empty($Data[$x])) {
                        $insertData[] = $Data[$x];
                    }
                }
                Db::table('dblow_premium')->insertAll($insertData);
            } else {
                Db::table('dblow_premium')->insertAll($five);
            }
        } else {
            Db::table('dblow_premium')->insertAll($five);
        }

        //发邮件
        //全部为3是建仓
        $status = Db::table('dblow_premium')
            ->column('status');

        $status = array_unique($status);
        if (in_array('2', $status)) {
            //卖出数据拿出来
            $sellhose = Db::table('dblow_premium')
                ->where('status', 2)
                ->select();

            //买入数据拿出来
            $buyhose = Db::table('dblow_premium')
                ->where('status', 3)
                ->select();

            $sell = [];
            $buy = [];
            //换成邮件内容需要的数组
            foreach ($buyhose as $key => $value) { //买入
                $buy[] = '代码：' . $value['bond_id'] . ' 名字：' . $value['bond_name'] . '买入价格：' . $value['price'] . '溢价率：' . $value['premium_rt'] . ' 类型：' . '前5';
            }
            foreach ($sellhose as $key => $value) { //卖出
                $sell[] = '代码：' . $value['bond_id'] . ' 名字：' . $value['bond_name'] . ' 买入价格：' . $value['price'] . ' 卖出价格：' . $value['new_price'] . '溢价率：' . $value['time_premium_rt'] . ' 类型：' . '前5';
            }
            $mailtext = '卖出:' . PHP_EOL . implode(PHP_EOL, $sell) . PHP_EOL . "买入" . PHP_EOL . implode(PHP_EOL, $buy);
            $res = email("前10低于170", "调仓", '卖出' . count($sell) . "支买入" . count($buy) . "支明细：" . PHP_EOL . $mailtext);
            if ($res == 200) {
                Db::startTrans();
                Db::table('dblow_premium')
                    ->where('status', 3)
                    ->update(['status' => 1]);

                Db::table('dblow_premium')
                    ->where('status', 2)
                    ->update(['status' => 4, 'updated_at' => time()]);
                Db::commit();
                echo '前10低于170调仓成功';
            }
        } elseif (in_array('3', $status)) {

            //建仓数据拿出来
            $createhose = Db::table('dblow_premium')
                ->where('status', 3)
                ->select();


            //换成邮件内容需要的数组
            foreach ($createhose as $key => $value) {
                $buy[] = '代码：' . $value['bond_id'] . ' 名字：' . $value['bond_name'] . ' 价格：' . $value['price'] . ' 类型：' . '前5';
            }
            $mailtext = "建仓:" . PHP_EOL . implode(PHP_EOL, $buy);
            $res = email("前10低于170", "建仓", "共" . count($buy) . "支明细：" . PHP_EOL . $mailtext);
            if ($res == 200) {
                Db::startTrans();
                Db::table('dblow_premium')
                    ->where('status', 3)
                    ->update(['status' => 1]);
                Db::commit();

                echo '前10低于170建仓成功';
            }
        } else {
            echo '没触发邮件';
        }
    }

    public
    function median($median) //https://www.jisilu.cn/data/cbnew/cb_index_quote/1
    {
        $price = array_column($median, 'price');
        sort($price);
        $totalNumbers = count($price);
        $mid = floor($totalNumbers / 2);
        $price = ($totalNumbers % 2) === 0 ? ($price[$mid - 1] + $price[$mid]) / 2 : $price[$mid];

        //获取数据
        $url = 'https://www.jisilu.cn/data/cbnew/cb_index_quote/1';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        $datas = json_decode($data, true);
        //        dump($datas);die;

        //中位数
        $price = $datas['mid_price'];
        //计算星级
        $median_price = Db::table('median')
            ->column('price');

        $min = min($median_price);
        $max = max($median_price);

        $value = ($price - $min) / ($max - $min);
        //计算星级（中位数）
        if ($value <= 0.2) {
            $star = 5;
        } elseif ($value > 0.2 && $value <= 0.4) {
            $star = 4;
        } elseif ($value > 0.4 && $value <= 0.6) {
            $star = 3;
        } elseif ($value > 0.6 && $value <= 0.8) {
            $star = 2;
        } else {
            $star = 0;
        }

        $dblow = array_sum(array_column($median, 'dblow')) / count(array_column($median, 'dblow'));
        //计算星级（双底均值）
        if ($dblow <= 150) {
            $position = 100;
        } elseif ($dblow > 150 && $dblow <= 155) {
            $position = 80;
        } elseif ($dblow > 155 && $dblow <= 160) {
            $position = 60;
        } elseif ($dblow > 160 && $dblow <= 165) {
            $position = 40;
        } elseif ($dblow > 165 && $dblow <= 170) {
            $position = 20;
        } else {
            $position = 0;
        }
        $is_mail = Db::table('median')
            ->where('created_at', date('Y-m-d'))
            ->value('is_mail');

        if (!$is_mail) {
            Db::startTrans();
            Db::table('median')
                ->insert([
                    'price' => $price,
                    'dblow' => $dblow,
                    'premium_rt' => $datas['avg_premium_rt'],
                    'timestamp' => time(),
                    'midprice_star' => $star,
                    'dblow_star' => $position / 20,
                    'created_at' => date('Y-m-d'),
                ]);
            if ($star == 2 || $position == 20) {
                $title = '可转债进入高估区间';
            } elseif ($star >= 4 || $position >= 80) {
                $title = '可转债进入播种区间';
            } elseif ($star == '清仓' || $position == '清仓') {
                $title = '可转债可以考虑清仓了';
            } else {
                $title = '可转债正常估值';
            }

            $res = email("可转债投资价值", $title, '今天可转债价格中位数:' . $price . '元，双低均值' . $dblow . PHP_EOL . '中位数投资价值:' . $star . "星级" . '，双低均值投资价值:仓位' . $position . '%，星级' . $position / 20);
            if ($res == 200) {
                Db::table('median')
                    ->where('created_at', date('Y-m-d'))
                    ->update([
                        'is_mail' => 1,
                    ]);
            }

            Db::commit();
        }
    }
}
